In Python, the str or string data type is used to represent a sequence of characters. Strings are created by enclosing characters in quotes. Python treats single quotes the same as double quotes.
Here’s how you can declare a string in Python:
declaring a string in Python example with single and double quotes
# Declare a string
str1 = 'Hello, World!'
print(str1)
str2 = "Python is fun."
print(str2)
Output
Hello, World!
Python is fun.
In these examples, str1 and str2 are both string variables. The print() function is used to print the value of these variables.
Creating Multiline Strings
You can create multiline strings using triple double quotes (""") or triple single quotes ('''). For example:
Create multiline strings using triple single quotes example in python
message = '''
Never gonna give you up
Never gonna let you down
'''
print(message)
Output
Never gonna give you up
Never gonna let you down
You can also create multi-line strings using '"'triple quotes:
Creating multi-line string in python with double quotes example
# Declare a multi-line string
str3 = """This is
a multi-line
string."""
print(str3)
Output
This is
a multi-line
string
In this example, str3 is a multi-line string. Each line is separated by a newline character (\n).
Immutability of Python Strings
Strings in Python are immutable, meaning their characters cannot be changed once created. For instance, the following code will raise an error:
Changing character in a string
message = 'Hola Amigos'
message[0] = 'H' # Raises TypeError: 'str' object does not support item assignment
However, you can assign a new string to the variable:
String Immutability example in python
message = 'Hola Amigos'
message = 'Hello Friends'
print(message)
Output
Hello Friends
you can create a new string based on an existing one:
Creating a string in python example
# Create a new string based on an existing one
str1 = 'Hello, World!'
str2 = "Python is fun."
str4 = str1 + " " + str2
print(str4)
Output
Hello, World! Python is fun.
In this example, str4 is a new string created by concatenating str1, a space, and str2.
Accessing String Characters
Indexing:
Treat strings as lists and use index values to access individual characters. For example:
Getting character from string using index in python
greet = 'hello'
print(greet[1])
Output
e
Negative Indexing:
Similar to lists, Python allows negative indexing for strings. For example:
Getting character using negative index in string - python negative indexing
greet = 'hello'
print(greet[-4])
Output
e
Slicing:
Use the slicing operator (:) to access a range of characters in a string. For example:
Slicing basic example in python
greet = 'Hello'
print(greet[1:4])
Output
ell
Comparing Two Strings:
We use the == operator to compare strings. If two strings are equal, it returns True; otherwise, False.
Comparing two strings in python example
str1 = "Hello, world!"
str2 = "I love Swift."
str3 = "Hello, world!"
print(str1 == str2)
print(str1 == str3)
Output
False
True
Python provides a number of built-in methods for manipulating strings, such as lower(), upper(), split(), replace(), and many more.
Summary
✦ A string in Python is a sequence of characters.
✦ Python does not have a character data type, a single character is simply a string with a length of 1.
✦ Strings in Python can be created using single quotes, double quotes, or even triple quotes.
✦ For example, ""Hello World"", 'Hello World', and """"""Hello World"""""" are all valid strings in Python.
✦ The triple quotes can be used to declare multiline strings in Python.
✦ Strings are immutable, meaning that once you have created a string, you cannot change it.
Remember, Python uses the str data type to improve the readability of code and make it consistent across the wide spectrum of Python code. Consistency within one module or function is the most important.